﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TANKWAR_alpha
{
    class TankPlayer:ObjElement
    {

        public TankPlayer(int left,int top,GameGDI _gameGDI)
        {
            this.Left = left;
            this.Top = top;
            this.Width = Constant.tankWidth;
            this.Height = Constant.tankHeight;
            this.Type = 0;
            this.Life = true;
            this.Speed = Constant.TankPlayerSpeed;
            this.Direction = MoveDirection.UP;
            this.GameDriver = _gameGDI;
        }

       //public  KeyValuePair<int,MoveDirection> MoveByPlayer(MoveDirection direct)
       // {
       //    if(direct==MoveDirection.UP)
       //    {
       //        if(Direction==MoveDirection.UP)
       //        {
       //            /*
       //             * 检测玩家坦克是否与边界、墙、地方坦克相撞
       //             */
       //            if (!HitBoundaryOrWallOrtank(this.Left, this.Top - this.Speed))
       //                return new KeyValuePair<int, MoveDirection>(1,MoveDirection.UP);
       //        }
       //        else
       //        {
       //            this.Direction = MoveDirection.UP;
       //            return new KeyValuePair<int, MoveDirection>(0,MoveDirection.UP);
       //        }
       //    }
       //    else if (direct == MoveDirection.DOWN)
       //    {
       //        if (Direction == MoveDirection.DOWN)
       //        {
       //            /*
       //             * 检测玩家坦克是否与边界、墙、地方坦克相撞
       //             */
       //            if (!HitBoundaryOrWallOrtank(this.Left, this.Top +this.Speed))
       //                return new KeyValuePair<int, MoveDirection>(1, MoveDirection.DOWN);//this.Top += this.Speed;
       //        }
       //        else
       //        {
       //            this.Direction = MoveDirection.DOWN;
       //            return new KeyValuePair<int, MoveDirection>(0, MoveDirection.UP);
       //        }
       //    }
       //    else if (direct == MoveDirection.LEFT)
       //    {
       //        if (Direction == MoveDirection.LEFT)
       //        {
       //            /*
       //             * 检测玩家坦克是否与边界、墙、地方坦克相撞
       //             */
       //            if (!HitBoundaryOrWallOrtank(this.Left - this.Speed, this.Top))
       //                return new KeyValuePair<int, MoveDirection>(1, MoveDirection.LEFT);//this.Left -= this.Speed;
       //        }
       //        else
       //        {
       //            this.Direction = MoveDirection.LEFT;
       //            return new KeyValuePair<int, MoveDirection>(0, MoveDirection.UP);
       //        }
       //    }
       //    else if (direct == MoveDirection.RIGHT)
       //    {
       //        if (Direction == MoveDirection.RIGHT)
       //        {
       //            /*
       //             * 检测玩家坦克是否与边界、墙、地方坦克相撞
       //             */
       //            if (!HitBoundaryOrWallOrtank(this.Left +this.Speed, this.Top))
       //                return new KeyValuePair<int, MoveDirection>(1, MoveDirection.RIGHT);//this.Left+= this.Speed;
       //        }
       //        else
       //        {
       //            this.Direction = MoveDirection.RIGHT;
       //            return new KeyValuePair<int, MoveDirection>(0, MoveDirection.UP);
       //        }

       //    }
       //    return new KeyValuePair<int, MoveDirection>(0,MoveDirection.UP);
       // }


        public  void Move()
        {
                int[] dir1 = { 0, 0, -5, 5 };
                int[] dir2 = { -5, 5, 0, 0 };
              
                this .Left+=dir1[(int)Direction];
                this.Top += dir2[(int)Direction];
        }

        public bool MoveByPlayerJudge(MoveDirection direct)
        {
            if (direct == MoveDirection.UP)
            {
                if (Direction == MoveDirection.UP)
                {
                    /*
                     * 检测玩家坦克是否与边界、墙、地方坦克相撞
                     */
                    if (!HitBoundaryOrWallOrtank(this.Left, this.Top - this.Speed))
                        return false;
                }
                else
                {
                    this.Direction = MoveDirection.UP;
                    return true;
                }
            }
            else if (direct == MoveDirection.DOWN)
            {
                if (Direction == MoveDirection.DOWN)
                {
                    if (!HitBoundaryOrWallOrtank(this.Left, this.Top + this.Speed))
                        return false;//this.Top += this.Speed;
                }
                else
                {
                    this.Direction = MoveDirection.DOWN;
                    return true;
                }
            }
            else if (direct == MoveDirection.LEFT)
            {
                if (Direction == MoveDirection.LEFT)
                {
                    
                    if (!HitBoundaryOrWallOrtank(this.Left - this.Speed, this.Top))
                        return false;//this.Left -= this.Speed;
                }
                else
                {
                    this.Direction = MoveDirection.LEFT;
                    return true ;
                }
            }
            else  
            {
                if (Direction == MoveDirection.RIGHT)
                {
                   
                    if (!HitBoundaryOrWallOrtank(this.Left + this.Speed, this.Top))
                        return false;//this.Left+= this.Speed;
                }
                else
                {
                    this.Direction = MoveDirection.RIGHT;
                    return true;
                }
               
            }
            return true;
        }
      

       private bool HitBoundaryOrWallOrtank(double  left, double top)
       {
           /**********判断是否超出了边界**************/
           if (left < 0 || left + Constant.tankWidth > Constant.clientWidth || top < 0 || top + Constant.tankHeight > Constant.clientHeight)
               return true;

           /************判断是否与墙或铁相碰**************/
           foreach(Wall wa in this.GameDriver.Walls)
           {
                  if (wa.WallType != 2&& wa.WallType != 4)
                    {
                   
                            if (Constant.RectangleIntersection(wa.WallLeft, wa.WallTop, Constant.WallWidth, Constant.WallHeight, left, top, Constant.tankWidth, Constant.tankHeight))
                            {
                                return true;
                            }
                    }
                  else  if(wa.WallType!=2)
                   {
                            if (Constant.RectangleIntersection(wa.WallLeft, wa.WallTop, Constant.BaseSize, Constant.BaseSize, left, top, Constant.tankWidth, Constant.tankHeight))
                            {
                                return true;
                            }
                   }
               
           }
            
           /**********判断是否与地方坦克相碰**************/
           foreach(TankRobot ta in this.GameDriver.TankRobots)
           {
               if (Constant.RectangleIntersection(ta.Left,ta.Top,Constant.tankWidth,Constant.tankHeight, left, top, Constant.tankWidth, Constant.tankHeight))
               {
                   return true;
               }
           }

           return false;

       }

        public void ShootByPlayer()
        {
            PlayMusic.Play(@"Music\ShootByPlayer.wav");
            Bullet b;
            switch(this.Direction)        //adjust the location and direction of bullet
            {
                case MoveDirection.UP: b = new Bullet(this.Left + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, Top - Constant.bombHeight, 0, MoveDirection.UP, this.GameDriver); break;
                case MoveDirection.DOWN: b = new Bullet(this.Left + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, Top + Constant.tankHeight+Constant .bombHeight, 0, MoveDirection.DOWN, this.GameDriver); break;
                case MoveDirection.LEFT: b = new Bullet(this.Left-Constant .bombWidth,this.Top+Constant .tankWidth /2.0-Constant .bombWidth/2.0,0,MoveDirection.LEFT,this.GameDriver); break;
                case MoveDirection.RIGHT: b = new Bullet(this.Left+Constant .tankWidth+Constant .bombWidth,this.Top+Constant .tankWidth /2.0-Constant .bombWidth/2.0,0,MoveDirection.RIGHT,this.GameDriver); break;
                default: b = null; break;        //can't happen

            }
            this.GameDriver.PlayerBullets.Add(b);

        }
    }
}
